#include "newfriendapplybtn.h"
#include "ui_newfriendapplybtn.h"

NewFriendApplyBtn::NewFriendApplyBtn(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::NewFriendApplyBtn)
{
    ui->setupUi(this);
    count=0;
    iswidgetshowing=false;
    ui->label_newcount->hide();
    setCount(FriendApplyLib::getInstance()->getNewApplyCount());
    connect(FriendApplyLib::getInstance(),&FriendApplyLib::s_haveNewApply,this,&NewFriendApplyBtn::addCount);
}

NewFriendApplyBtn::~NewFriendApplyBtn()
{
    delete ui;
}

void NewFriendApplyBtn::addCount()
{
    if(iswidgetshowing)return;
    count++;
    checkCount();
}

void NewFriendApplyBtn::setCount(const int num)
{
    count=num;
    checkCount();
}


int NewFriendApplyBtn::getCount()
{
    return count;
}

void NewFriendApplyBtn::setUseroc(const QString &useroc)
{
    this->useroc=useroc;
}

void NewFriendApplyBtn::checkCount()
{
    if(count>0&&!iswidgetshowing)
    {
        ui->label_newcount->setNum(count);
        ui->label_newcount->show();
    }
    else
        ui->label_newcount->hide();
}

void NewFriendApplyBtn::setParent(QWidget *parent)
{
    this->parent=parent;
}

void NewFriendApplyBtn::applyWidgetClose()
{
    iswidgetshowing=false;
}


void NewFriendApplyBtn::on_pushButton_clicked()
{
    count=0;
    iswidgetshowing=true;
    checkCount();
    FriendApplyLib::getInstance()->resetNewCount();
    FriendApplyWidget* widget=new FriendApplyWidget(useroc);
    widget->setParent(this->parent);
    connect(widget,&FriendApplyWidget::s_widgetclose,this,&NewFriendApplyBtn::applyWidgetClose,Qt::UniqueConnection);
    widget->setModal(true); // 设置为模态对话框
    widget->show();
    widget->exec(); // 使用exec()方法使窗口变为模态，阻塞父窗口
}
